package com.example.pkmedical.Activity.warning;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class LeaveBedOkActivity extends BaseActivity {

    @BindView(R.id.leave_bedok_text)
    TextView leaveBedokText;
    @BindView(R.id.leave_bedok_button)
    Button leaveBedokButton;
    @BindView(R.id.leavebedok_img)
    ImageView leavebedokImg;
    @BindView(R.id.headtilt_text)
    TextView headtiltText;
    @BindView(R.id.id_leavebed_layout)
    RelativeLayout idLeavebedLayout;
    private int mType = 0;
    private int timeLongFS = 2;

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (mTimeCountDisposableFS != null) {
//            mTimeCountDisposableFS.dispose();
//        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leavebedok);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        switch (mType) {
            case 0:
                switch (CommandUtil.HEAD_WARNING_TYPE) {
                    case 1:
                        leaveBedokText.setText(getString(R.string.leavebed_ok_30_ok));
                        leavebedokImg.setImageResource(R.drawable.img_headwarn_three);

                        break;
                    case 2:
                        leaveBedokText.setText(getString(R.string.leavebed_ok_45_ok));
                        leavebedokImg.setImageResource(R.drawable.img_headwarn_four);
                        break;
                }
                break;
            case 1:
                switch (CommandUtil.LEAVEBED_TYPE) {
                    case 1:
                        leaveBedokText.setText(getString(R.string.leavebed_ok_position_type_ok));
                        leavebedokImg.setImageResource(R.drawable.img_staring_right);
                        break;
                    case 2:
                        leaveBedokText.setText(getString(R.string.leavebed_ok_ing_type_ok));
                        leavebedokImg.setImageResource(R.drawable.img_leaveing);
                        break;
                    case 3:
                        leaveBedokText.setText(getString(R.string.leavebed_ok_leaved_type_ok));
                        leavebedokImg.setImageResource(R.drawable.img_leaved);
                        break;
                }
                break;
        }

    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            mType = intent.getIntExtra("warning_type", 0);
        }
        TimeCountFS();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick(R.id.leave_bedok_button)
    public void onViewClicked() {
        LeaveBedOkActivity.this.finish();
    }

    /**
     * 倒计时
     */

    private Disposable mTimeCountDisposableFS;

    public void TimeCountFS() {
        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposableFS = Flowable.intervalRange(0, timeLongFS, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {

                })
                .doOnComplete(() -> {
                    int mAngle = SPUtils.getInstance().getInt("bed_final_angle", 0);
                    Log.d("yangshuoHEADQINGJIAO",mAngle+"****************************"+CommandUtil.HEAD_WARNING_TYPE);
                    if (0 == mType) {
                        switch (CommandUtil.HEAD_WARNING_TYPE) {
                            case 1:
                                if (mAngle < 30) {
                                    startActivity(new Intent(MyApp.getAppContext(), WarningCenterActivity.class));
                                    LeaveBedOkActivity.this.finish();
                                }
                                break;
                            case 2:
                                if (mAngle < 45) {
                                    startActivity(new Intent(MyApp.getAppContext(), WarningCenterActivity.class));
                                    LeaveBedOkActivity.this.finish();
                                }
                                break;
//                        case 0:
//                            LeaveBedOkActivity.this.finish();
//                            break;
                        }
                    }
                    if (mTimeCountDisposableFS != null) {
                        mTimeCountDisposableFS.dispose();
                    }
                })
                .subscribe();
    }


}